package com.suneee.ep.proxy.utils;


import java.security.MessageDigest;
import java.util.Base64;
import java.util.Map;
import java.util.TreeMap;
import java.util.Base64.Decoder;
import java.util.Base64.Encoder;
import java.util.HashMap;
import java.util.Iterator;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.suneee.ep.proxy.api.ApiData;
import com.suneee.ep.proxy.api.ProxyData;
 
public class Utils {
	
	public static String toJson(Object v){
		String s=JSON.toJSONString(v);
		return s;
	}
	
	public static <T> T parseJson(String json,Class<T> clazz){		
		T v=JSON.parseObject(json,clazz);
		return v;
	}
	
	public static <T> T parseJson(String json,TypeReference<T> type){		
		T v=JSON.parseObject(json,type);
		return v;
	}
	
	public static String md5(String v){
		String r=null;
		try{
			MessageDigest md5=MessageDigest.getInstance("MD5");
			byte []b=md5.digest(v.getBytes("utf-8"));
			r=base64(b);
		}catch(Exception e){
			e.printStackTrace();
		}
		return r;
	}
	
	public static String base64(byte []b){
		Encoder ec=Base64.getEncoder();
		byte[] v=ec.encode(b);
		String s=new String(v);
		return s;
	}
	
	public static byte[] fromBase64(String v){
		Decoder ec=Base64.getDecoder();
		byte[] b=ec.decode(v);
		return b;
	}
	
	public static ApiData decode(String ds) throws Exception{
		if(ds==null || ds.length()==0){
			return null;
		}
		ApiData d=null;
		String s=decode(ds,2,5); //解密
		byte []bs=Utils.fromBase64(s); //经过  base64编码的值
		String json=new String(bs,"utf-8");		
		d=Utils.parseJson(json, ApiData.class);
		
		return d;
	} 
	
	public static String encode(ApiData v) throws Exception{
		if(v==null){
			return null;
		}
		String json=toJson(v);
		byte []b=json.getBytes("utf-8");
		String bs=base64(b);
		String rs=encode(bs,2,5);
		return rs;
	}
	
	
	
	private static String en(String v,int n){
		if(n<1){
			return v;
		}
		StringBuffer sb=new StringBuffer();
		String sub;
		int num=v.length()/n; 
		for(int i=0;i<num;i++){
			sub=v.substring(i*n,(i+1)*n);
			sb.append(sub.substring(1));
			sb.append(sub.substring(0,1));			
		}
		num=v.length()%n;
		if(num>0){
			sb.append(v.substring(v.length()-num));
		}
		return sb.toString();
	}
	
	private static String de(String v,int n){
		if(n<1){
			return v;
		}
		StringBuffer sb=new StringBuffer();
		String sub;
		int num=v.length()/n; 
		for(int i=0;i<num;i++){
			sub=v.substring(i*n,(i+1)*n);
			sb.append(sub.charAt(sub.length()-1));
			sb.append(sub.substring(0,sub.length()-1));
		}
		num=v.length()%n;
		if(num>0){
			sb.append(v.substring(v.length()-num));
		}
		return sb.toString();
	}
	
	public static String encode(String v,int len,int num){
		String s=v;
		for(int i=len;i<len+num;i++){
			s=en(s,i);
		}
		return s;
	}
	
	public static String decode(String v,int len,int num){
		String s=v;
		int to=len+num;
		while(to>len){
			s=de(s,to-1);
			to--;
		}
		return s;
	}
	
	public static ProxyData createProxyData(ApiData v){
		String json=toJson(v);
		ProxyData d=new ProxyData();
		d.setData(json);
		
		d.setSign("sign");
		return d;
	}
	
	
	public static <T> TreeMap<String,T> toTreeMap( Map<String,T> v){
		TreeMap<String,T> m=new TreeMap<String,T>();
		m.putAll(v);
		return m;
	}
	
	public static TreeMap<String,String> convert( Map<String,Object> m){
		TreeMap<String,String> r=new TreeMap<String,String>();
		Iterator<String> it = m.keySet().iterator();
		String k,v;
		while (it.hasNext()){
			k=it.next();
			v=null;
			if(m.get(k)!=null){
				v=m.get(k).toString();
				r.put(k,v);
			}
		}
		return r;
	}
	
	private static void testEncode(){
		String po="This is a test,这个世界的伟大之处在于自我修复的能力，打破的平衡必将再次建立"; 
		String v=base64(po.getBytes());
		String v1=encode(v,2,5); 
		System.out.println(v1); 
		String d=decode(v1,2,5);
		System.out.println(d);
		if(v.equals(d)){
			System.out.println(true);
		}else{
			System.out.println(false);
		}
		String p=new String(fromBase64(v));
		System.out.println(p);
	}
	
	private static void testJs()  throws Exception {
		ApiData a=new ApiData();
		a.setDesc("这是描述");
		a.setStatus("状态");
		Map<String, String> hs=new HashMap<String, String>();
		hs.put("proxy", "ie-chrome");
		a.setHead(hs);
		Map<String, Object> bs=new HashMap<String, Object>();
		bs.put("body","这里有消息的内容");
		a.setBody(bs);
		
		String msg=encode(a);
		System.out.println(msg);
		
		ApiData b=decode(msg);
		
		System.out.println(toJson(a));
		System.out.println(toJson(b));
	}
	
	private static void testMap(){
		HashMap<String,String> m1=new HashMap<String,String>();
		m1.put("A","A001");
		m1.put("B","B001");
		m1.put("C","C001");
		
		m1.put("1","1001");
		m1.put("2","2001");
		m1.put("3","3001");
		
		System.out.println(toJson(m1));
		
		TreeMap<String,String> tm=toTreeMap(m1);
		
		System.out.println(toJson(tm));
		
	}
	public static void main(String []args) throws Exception {
		testMap();
	}
	 
}
